
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ReturnListener;
import com.rabbitmq.client.MessageProperties;
import com.rabbitmq.client.AMQP;
import java.io.IOException;


public class Mandatory_producer{
	public static void main(String[] args) throws Exception {
            ConnectionFactory cf = new ConnectionFactory();
            cf.setHost("192.168.1.105");
            cf.setUsername("liwenbin");
            cf.setPassword("123456");
            Connection connection = cf.newConnection();
            Channel channel = connection.createChannel();
            String message = "Hello World!";
            String QUEUE_NAME = "hello2";
            channel.addReturnListener(new ReturnListener() {
                public void handleReturn(int replyCode,
                                  String replyText,
                                  String exchange,
                                  String routingKey,
                                  AMQP.BasicProperties basicproperties,
                                  byte[] body) throws IOException{
                     String message = new String(body);
                     System.out.println("Basic.Return返回的结果是："+ message);
                }
            });
            channel.basicPublish("", QUEUE_NAME, true, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
            System.out.println("publish ok");
//            channel.close();
//            connection.close();
	}
}

